mui.init();
var imgBase64; // 当前图片的编码
var filesName; // 当前图片的图片名
var inIndex;
$(function() {
	var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"></li>',
		$gallery = $("#gallery"),
		$galleryImg = $("#galleryImg"),
		$uploaderInput = $("#uploadImage"),
		$uploaderFiles = $("#uploaderFiles");

	$uploaderInput.on("change", function(e) {
		var src, url = window.URL || window.webkitURL || window.mozURL,
			files = e.target.files;
		for(var i = 0, len = files.length; i < len; i++) {
			var file = files[i];
			if(url) {
				src = url.createObjectURL(file);
			} else {
				src = e.target.result;
			}
			if(files.length != "1") {
				$.toptip("请不要一次上传多张图片，谢谢！！", 'error');
				return false;
			} else {
				sizes = (files[0].size) / 1024;
				if(sizes < 1024) {
					// 把选中的图片显示在加号上
					//				$uploaderFiles.append($(tmpl.replace('#url#', src)));
					ajaxFileUpload($uploaderFiles, $(tmpl.replace('#url#', src)));
				} else {
					$.toptip("图片上传大小超过1M，请上传小于1M的图片，谢谢！！", 'error');
				}
			}
		}
	});
	var index; //第几张图片 
	$uploaderFiles.on("click", "li", function() {
		index = $(this).index();
		$galleryImg.attr("style", this.getAttribute("style"));
		$gallery.fadeIn(100);
		console.log("显示查看第" + (index + 1) + "张图片成功");
	});
	$gallery.on("click", function() {
		$gallery.fadeOut(100);
		console.log("关闭查看第" + (index + 1) + "张图片成功")
	});
	//删除图片  
	$(".weui-gallery__del").click(function() {
		$uploaderFiles.find("li").eq(index).remove();
		console.log("删除第" + (index + 1) + "张图片成功");
		$("#uploader").css("display", "block");
		$(".p10").css("display", "block");
		$(".weui-cells_form").css("display", "block");
		if(window.confirm('您确定要删除当前图片吗？')) {
			succIndex = succIndex - 1;
			if(index == 2) {
				$(".weui-uploader__input-box").css("display", "block");;
			}
			return true;
		} else {
			return false;
		}
	});
});

// 成功上传到哪张图片
var succIndex = 0;

/**
 * 身份证上传功能
 */
function ajaxFileUpload(el, str) {
	var formdata = new FormData();
	formdata.append('uploadImage', $('#uploadImage').get(0).files[0]);
	$.ajax({
		url: ip + "dfs/dfs/upload.dfs",
		type: 'post',
		contentType: false,
		data: formdata,
		processData: false,
		success: function(data) {
			// 0是成功，其他都是失败
			if(data.statusCode) {
				alert(data.resultDesc);
			} else {
				// 图片替换加号显示
				el.append(str);
				alert('上传成功');
				succIndex++;
				if(succIndex == 1) {
					remoteFileName1 = "/" + JSON.parse(data).result[0].item.groupName + "/" + JSON.parse(data).result[0].item.remoteFileName;
				} else if(succIndex == 2) {
					remoteFileName2 = "/" + JSON.parse(data).result[0].item.groupName + "/" + JSON.parse(data).result[0].item.remoteFileName;
				} else if(succIndex == 3) {
					remoteFileName3 = "/" + JSON.parse(data).result[0].item.groupName + "/" + JSON.parse(data).result[0].item.remoteFileName;
					$(".weui-uploader__input-box").css("display", "none");
				}
			}
		},
		error: function(err) {
			// 不处理
		}
	});
}

/**
 * 确认上传
 */
var data = localStorage.getItem("data");
var obj = JSON.parse(data);
var remoteFileName1;
var remoteFileName2;
var remoteFileName3;
var first = true;

function getRealNameAuthentication() {
	//$(".weui-btn").attr("disabled", false);
	if($("#userName").val().trim() == '') {
		$.toast("姓名不能为空!", "text");
		return false;
	}
	if($("#card").val().trim() == '') {
		$.toast("身份证号码不能为空!", "text");
		return false;
	}
	if(remoteFileName1 == null) {
		$.toast("身份证照片不能为空!", "text");
		return false;
	}
	if(remoteFileName2 == null) {
		$.toast("身份证照片不能为空!", "text");
		return false;
	}
	if(remoteFileName3 == null) {
		$.toast("身份证照片不能为空!", "text");
		return false;
	}
	var getRealNameAuthenticationRecord = {
		'userId': id,
		'1': remoteFileName1,
		'2': remoteFileName2,
		'3': remoteFileName3,
		'trueName': $("#userName").val().trim(),
		'idCard': $("#card").val().trim()
	}
	if(first) {
		first = false;
		$.ajax({
			type: "POST",
			url: ip + "platform/user/setIdCardVerify.m",
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			data: getRealNameAuthenticationRecord,
			success: function(data) {
				if(data.statusCode == 0) {
					//$(".weui-btn").attr("disabled", true);
					$.toptip("提交实名认证成功！", 'success');
					setTimeout('window.location.href = "securitySetting.html"', 100);
				} else if(data.statusCode == 1000) {
					$.toptip("登录失效！", 'error');
					setTimeout('window.location.href = "signIn.html"', 1000);
				} else {
					$.toptip(data.resultDesc, 'error');
					first = true;
				}
			}
		});
	}
}
/**
 * 查询是否认证
 */
$(document).ready(function() {
	serverGetUserInfo();
});
var id;

function serverGetUserInfo() {
	$.ajax({
		type: "GET",
		url: ip + "platform/user/serverGetUserInfo.m",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		success: function(data) {
			if(data.statusCode == 0) {
				id = data.result.id;
				var imgStatus = data.result.imgStatus;
				if(imgStatus == 1) {
					$("#sh").text("审核成功！");
					$("#uploader").css("display", "none");
					$(".p10").css("display", "none");
					$("#userName").attr("disabled", true);
					$("#card").attr("disabled", true);
					$("#userName").css("color", "#04BE02");
					$("#card").css("color", "#04BE02");
					$("#icf").html('<i class="weui-icon-success"></i><span style="color:#04BE02">实名认证成功</span>');
					$(".weui-cells_form").css("display", "none");
					$("#sh").css("color", "blue");
				} else if(imgStatus == 2) {
					$("#sh").text(data.result.idcardVerifyDesc);
					$("#sh").css("color", "red");
				} else if(imgStatus == 3) {
					$("#sh").text("审核中，请稍候...");
					$("#uploader").css("display", "none");
					$(".p10").css("display", "none");
					$("#userName").attr("disabled", true);
					$("#card").attr("disabled", true);
					$("#sh").css("color", "orange");
				} else {
					return;
				}
				$("#userName").val(data.result.trueName);
				$("#card").val(data.result.idCard);
			} else if(data.statusCode == 1000) {
				$.toptip("登录失效！", 'error');
				setTimeout('window.location.href = "signIn.html"', 1000);
			} else {
				$.toptip(data.resultDesc, 'error');
			}
		}
	});
}